package redis.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import redis.limiter.RedisTokenBucketRateLimiter;

import javax.annotation.Resource;

/**
 * @author zyl
 * @description
 * @since 2025/8/22
 */
@Configuration
public class RateLimiterConfig {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    // API限流器
    @Bean
    public RedisTokenBucketRateLimiter apiRateLimiter() {
        return new RedisTokenBucketRateLimiter(
                redisTemplate,
                "api:rate_limit",
                20,    // 突发20次
                10.0   // 每秒10次
        );
    }

    // 登录限流器
    @Bean
    public RedisTokenBucketRateLimiter loginRateLimiter() {
        return new RedisTokenBucketRateLimiter(
                redisTemplate,
                "login:rate_limit",
                5,     // 突发5次
                1.0    // 每秒1次
        );
    }
}
